package com.lwapp.user.controller;

import com.lwapp.entity.Result;
import com.lwapp.entity.StatusCode;
import com.lwapp.user.pojo.Admin;
import com.lwapp.utils.JwtUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;
import java.util.HashMap;
import java.util.Map;


@RestController
@CrossOrigin
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private JwtUtil jwtUtil=null;
    @Autowired
    HttpServletRequest request;

    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public Result add(){

        return new Result(true, StatusCode.OK,"添加成功");
    }
    @RequestMapping(value = "/login",method =RequestMethod.POST)
    public Result login(@RequestBody Admin admin){
        System.out.println(admin.toString());
        String token = jwtUtil.createJWT(admin.getId(), admin.toString(), "admin");//获得令牌
        Map<String, Object> map =new HashMap<>();
        map.put("token",token);
        map.put("roles","admin");
        return new Result(true, StatusCode.OK,"登陆成功",map);
    }
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
    public Result delete(@PathVariable("id") String id){
        //
        String  token = (String)request.getAttribute("claims_admin");
        System.out.println();
        if(StringUtils.isEmpty(token)){
            return new Result(false, StatusCode.ACCESSERROR,"权限不足");
        }
        return new Result(true, StatusCode.OK,"删除成功");
    }

    @RequestMapping(value = "/other",method =RequestMethod.POST)
    public Result other(){

        return new Result(true, StatusCode.OK,"其他成功");
    }
}
